package com.stock_t.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stock_t.entity.AccountSummary;
import com.stock_t.page.TableDataInfo;
import com.stock_t.service.AccountSummaryService;
import com.stock_t.utils.PageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 账户汇总控制器
 */
@Slf4j
@RestController
@RequestMapping("/account/summary")
public class AccountSummaryController {

    @Autowired
    private AccountSummaryService accountSummaryService;

    /**
     * 获取账户汇总列表
     */
    @PostMapping("/list")
    public Map<String, Object> list(@RequestBody AccountSummary accountSummary) {
        QueryWrapper<AccountSummary> queryWrapper = new QueryWrapper<>(accountSummary);
        List<AccountSummary> list = accountSummaryService.list(queryWrapper);
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("data", list);
        result.put("msg", "success");
        return result;
    }

    /**
     * 分页查询账户汇总
     */
    @PostMapping("/page")
    public TableDataInfo page(@RequestBody AccountSummary accountSummary) {
        // 使用PageUtils开启分页
        Page<AccountSummary> page = PageUtils.startPage();
        log.info("分页查询账户汇总，查询条件：{}", accountSummary);
        // 构建查询条件
        QueryWrapper<AccountSummary> queryWrapper = new QueryWrapper<>(accountSummary);
        accountSummaryService.page(page, queryWrapper);
        
        return PageUtils.getDataTable(page);
    }
    
    /**
     * 根据ID获取账户汇总
     */
    @GetMapping("/{id}")
    public Map<String, Object> getById(@PathVariable("id") Long id) {
        AccountSummary accountSummary = accountSummaryService.getById(id);
        
        Map<String, Object> result = new HashMap<>();
        if (accountSummary != null) {
            result.put("code", 200);
            result.put("data", accountSummary);
            result.put("msg", "success");
        } else {
            result.put("code", 404);
            result.put("msg", "账户汇总不存在");
        }
        
        return result;
    }
    
    /**
     * 保存账户汇总（新增或更新）
     */
    @PostMapping("/save")
    public Map<String, Object> save(@RequestBody AccountSummary accountSummary) {
        boolean success = accountSummaryService.saveOrUpdate(accountSummary);
        
        Map<String, Object> result = new HashMap<>();
        if (success) {
            result.put("code", 200);
            result.put("msg", "保存成功");
        } else {
            result.put("code", 500);
            result.put("msg", "保存失败");
        }
        
        return result;
    }
    
    /**
     * 更新账户汇总
     */
    @PutMapping("/update")
    public Map<String, Object> update(@RequestBody AccountSummary accountSummary) {
        // 确保记录存在
        AccountSummary existAccountSummary = accountSummaryService.getById(accountSummary.getId());
        
        Map<String, Object> result = new HashMap<>();
        if (existAccountSummary == null) {
            result.put("code", 404);
            result.put("msg", "账户汇总不存在");
            return result;
        }
        
        boolean success = accountSummaryService.updateById(accountSummary);
        
        if (success) {
            result.put("code", 200);
            result.put("msg", "更新成功");
        } else {
            result.put("code", 500);
            result.put("msg", "更新失败");
        }
        
        return result;
    }
    
    /**
     * 删除账户汇总
     */
    @DeleteMapping("/{id}")
    public Map<String, Object> delete(@PathVariable("id") Long id) {
        // 确保记录存在
        AccountSummary existAccountSummary = accountSummaryService.getById(id);
        
        Map<String, Object> result = new HashMap<>();
        if (existAccountSummary == null) {
            result.put("code", 404);
            result.put("msg", "账户汇总不存在");
            return result;
        }
        
        boolean success = accountSummaryService.removeById(id);
        
        if (success) {
            result.put("code", 200);
            result.put("msg", "删除成功");
        } else {
            result.put("code", 500);
            result.put("msg", "删除失败");
        }
        
        return result;
    }
    
    /**
     * 批量删除账户汇总
     */
    @DeleteMapping("/batch")
    public Map<String, Object> batchDelete(@RequestBody List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            Map<String, Object> result = new HashMap<>();
            result.put("code", 400);
            result.put("msg", "请选择要删除的记录");
            return result;
        }
        
        boolean success = accountSummaryService.removeByIds(ids);
        
        Map<String, Object> result = new HashMap<>();
        if (success) {
            result.put("code", 200);
            result.put("msg", "批量删除成功");
        } else {
            result.put("code", 500);
            result.put("msg", "批量删除失败");
        }
        
        return result;
    }
}